package cn.wolfcode.mp.util;


import cn.wolfcode.mp.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 抽取常量工具类
 */
public class UserContext {

    //抽取常量 session中使用的key值
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    // 使用springmvc提供工具类,获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest().getSession();
    }

    public static void setCurrentUser(Employee employee){
        //把用户信息存到session中
        getSession().setAttribute(USER_IN_SESSION,employee);
    }
    public static void setPermissions(List<String> list){
        //把权限信息存到session中
        getSession().setAttribute(PERMISSION_IN_SESSION,list);
    }

    public static Employee getCurrentUser(){
        //从session中取用户的信息
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    public static List<String> getPermissions(){
        //从session中取权限的信息
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }
}
